//
//  Extension+UIImage.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/28.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit

extension UIImage {
    
    
    //获取截屏结果
    static func getScreenShot()->UIImage{
        
        //得到 windows
        let windows = UIApplication.sharedApplication().keyWindow!
        //开启上下文
        UIGraphicsBeginImageContext(windows.bounds.size)
        // 01参数: size  02参数 是否透明 03参数 倍数【0,1】
        UIGraphicsBeginImageContextWithOptions(windows.bounds.size, false, 0)
        //将window 渲染到上下文中
        windows.drawViewHierarchyInRect(windows.bounds, afterScreenUpdates: false)
        //从上下文中获取 image
        let  image = UIGraphicsGetImageFromCurrentImageContext()
        //关闭上下文
        UIGraphicsEndImageContext()
        //返回图片
        return image
    }
    
    
    //压缩图片
    func getImageScale(width:CGFloat) -> UIImage{
        
        //如果当前图片的宽度小于程序员指定的宽度
        if  self.size.width < width {
            
            return self
        }
        
        //获取图片的最终高度
        let height = (width * self.size.height)/self.size.width
        //设置 rect
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        //开启上下文
        UIGraphicsBeginImageContext(rect.size)
        //将图片渲染到上下文中
        self.drawInRect(rect)
        // 从上下文中获取 image
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        //关闭上下文
        UIGraphicsEndImageContext()
        //返回图片
        return result
    }
    
    //获得 emotion 的 image
    class func getEmoticonImage(path:String?) ->UIImage?{
       
        
        return   UIImage(named: path ?? "", inBundle: HYFEmoticonTools.sharedTools.emotionBundle, compatibleWithTraitCollection: nil)
    }
    
    
}

